﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Viper;
using diamondc;

namespace cobaltc
{
    class Program
    {
        static void Main(string[] args)
        {
            string code = @"
class Program
    void main()
        string str = " + "\" s\"" + @"
        int i = &str
        Textscreen.Writeln(i)
        *i = " + "\"" + @"Hello" + "\"" + @"
        TextScreen.Writeln(str)
    end
end
";
            int i = 0;
            if (args.Length != 0)
            {
                string source = args[0];
                StreamReader sr = new StreamReader(args[0]);
                code = sr.ReadToEnd();
                code = code.Replace("\r", "");

                Scanner s = new Scanner(new System.IO.StringReader(code));

                compiler c = new compiler(s.Tokens);

                c.compile();
                Assembler asm = new Assembler();
                asm.Assemble(c.ILCode);
                asm.assembly.CreateGEX(System.IO.Path.GetFileNameWithoutExtension(args[0]) + ".gex");

                return;
            }
            while (true)
            {
                if (i.ToString().Length == 2)
                {
                    
                    Console.Write("0" + i.ToString() + ":");
                }
                if (i.ToString().Length == 1)
                {
                    Console.Write("00" + i.ToString() + ":");
                }
                string asmc = Console.ReadLine();
                if (asmc != "run")
                {

                    code += asmc + "\n";
                }
                else
                {
                    break;
                }
                i++;

            }
            if (true)
            {
                Scanner s = new Scanner(new System.IO.StringReader(code));

                compiler c = new compiler(s.Tokens);

                c.compile();
                Assembler asm = new Assembler();
                asm.Assemble(c.ILCode);
                Console.WriteLine(c.ILCode);
                Console.ReadLine();
                viperv3.ViperVirtualMachine vm = new viperv3.ViperVirtualMachine();
                Console.Clear();
                vm.Run(asm.assembly);

                Console.ReadLine();
            }
        }
    }
}
